<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
</head>

<body>
    <header class="aui-bar aui-bar-nav" style="background-color: #03a9f4;position:fixed;" id="header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            墨水瓶绑定
        </div>
    </header>
    <div class="aui-content aui-margin-b-15" style="margin-top:50px;">
        <ul class="aui-list aui-form-list">
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">墨水瓶</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="请 录入/扫描 墨水瓶标签" id="eslCode" value="" onclick="getIdSelector();">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">箱号</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="请扫描箱码" id="boxnum" value="" onclick="getIdSelector();">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">唯一标识</div>
                    <div class="aui-list-item-input">
                        <input type="text" id="tmID" disabled="true">
                    </div>
                </div>
            </li>
            <li class="aui-list-item" style="display:none">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">条码</div>
                    <div class="aui-list-item-input">
                        <input type="text" id="tm">
                    </div>
                </div>
            </li>
            <li class="aui-list-item" style="display:none">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">物料号</div>
                    <div class="aui-list-item-input">
                        <input type="text" id="mrlCode" disabled="true">
                    </div>
                </div>
            </li>
            <li class="aui-list-item" style="display:none">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">批次号</div>
                    <div class="aui-list-item-input">
                        <input type="text" id="lotCode" disabled="true">
                    </div>
                </div>
            </li>
            <li class="aui-list-item" style="display:none">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">数量</div>
                    <div class="aui-list-item-input">
                        <input type="number" id="qty" disabled="true">
                    </div>
                </div>
            </li>
        </ul>
        <p></p>
        <p></p>
        <div class="aui-btn aui-btn-info aui-btn-block" onclick="save()">保存</div>
    </div>
    <footer class="aui-bar aui-bar-tab" id="footer">
        <div class="aui-bar-tab-item" tapmode id="scan" onclick="doScan()">
            <i class="aui-iconfont aui-icon-scan aui-text-epblue" style="font-size: 1.8rem"></i>
        </div>
    </footer>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript">
    //光标所在id
    var idSelector = "";

    //初始化查询仓库下拉框
    apiready = function() {
        $("#eslCode").focus();
        idSelector = "eslCode";
    }


    // 获取光标所在组件的id
    function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        idSelector = el.id;
        $("#" + el.id).select();
    }

    //扫描
    function doScan() {
        var moduleScanner = api.require('moduleScanner');
        var params = {
            top: 150, //扫描窗口的上边距
            width: 250, //扫描窗口的宽
            height: 250, //扫描窗口的高
            useColor: false, //是否使用自己设定的颜色(startColor和endColor),
            //如果不使用设定的颜色就使用默认的那张网格图
            startColor: '#0F08C8FF', //渐变起始颜色
            endColor: '#7F08C8FF', //渐变结束颜色
            animTime: 2200, //扫描动画时间,单位为毫秒
        };
        moduleScanner.toScannerActivityForResult(params, function(ret, err) {
            //1：墨水瓶标签
            if (idSelector == 'eslCode') {

                $("#eslCode").val(ret);
                //自动保存
                saveAuto();

                //2:箱码
            } else if (idSelector == 'boxnum') {

                tmSplit(ret);

            }
        });
    }


    //保存按钮
    function save() {
        //墨水瓶标签
        var eslCode = $("#eslCode").val();
        //箱码
        var boxnum = $("#boxnum").val();
        //箱码
        var tmID = $("#tmID").val();
        //条码
        var tm = $("#tm").val();
        //物料号
        var mrlCode = $("#mrlCode").val();
        //批次号
        var lotCode = $("#lotCode").val();
        //数量
        var qty = $("#qty").val();


        //墨水瓶标签非空校验
        if (eslCode == "" || eslCode == null || eslCode == undefined) {
            api.toast({
                msg: '请 录入/扫描 墨水瓶标签',
                duration: 3000
            });
            return;
        }

        //箱号非空校验
        if (boxnum == "" || boxnum == null || boxnum == undefined) {
            api.toast({
                msg: '请扫描箱码',
                duration: 3000
            });
            return;
        }

        //唯一号非空校验
        if (tmID == "" || tmID == null || tmID == undefined) {
            api.toast({
                msg: '唯一标识不能为空',
                duration: 3000
            });
            return;
        }

        api.showProgress({
            title: '保存中...',
        });
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/uwmEslBindSave";
        var data = {
            values: {
                eslCode: eslCode, //墨水瓶标签
                boxnum: boxnum, //箱码
                tmID: tmID, //唯一号
                tm: tm, //条码
                mrlCode: mrlCode, //物料号
                lotCode: lotCode, //批次号
                qty: qty, //转移数量
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function(ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {

                    api.hideProgress();
                    api.toast({
                        msg: "保存成功",
                        duration: 3000
                    });

                    //清空表单
                    $("#eslCode").val("");
                    $("#boxnum").val("");
                    $("#tmID").val("");
                    $("#tm").val("");
                    $("#mrlCode").val("");
                    $("#lotCode").val("");
                    $("#qty").val("");

                } else if (myobj.code == '1') {
                    api.hideProgress();
                    $("#boxnum").val("");
                    $("#tmID").val("");
                    idSelector = "boxnum";
                    $("#boxnum").select();
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }

    //自动保存
    function saveAuto() {

        //墨水瓶标签
        var eslCode = $("#eslCode").val();
        //箱码
        var boxnum = $("#boxnum").val();
        //箱码
        var tmID = $("#tmID").val();

        //墨水瓶标签非空校验
        if (eslCode == "" || eslCode == null || eslCode == undefined) {
            return;
        }

        //箱号非空校验
        if (boxnum == "" || boxnum == null || boxnum == undefined) {
            return;
        }

        //唯一号非空校验
        if (tmID == "" || tmID == null || tmID == undefined) {
            return;
        }

        //校验通过保存
        save();
    }

    //扫描之后条码解析
    function tmSplit(sm) {
        $("#tm").val(sm)
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/analysisBarCodeCommon";
        var data = {
            values: {
                tm: sm, //条码
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function(ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {

                    var map = myobj.data;
                    for (var key in map) {
                        $("#" + key).val(map[key]);
                    }

                    //自动保存
                    saveAuto();

                } else if (myobj.code == '1') {

                    $("#boxnum").val("");
                    $("#tmID").val("");
                    idSelector = "boxnum";
                    $("#boxnum").select();
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                alert(JSON.stringify(err));
            }
        });
    }

    //箱号监听回车事件
    $("#boxnum").on('keypress', function(e) {
        var keycode = e.keyCode;
        var tm = $(this).val();
        if (keycode == '13') {

            //调用条码解析+保存事件
            tmSplit(tm);
        }
    });

    //墨水瓶标签码监听回车事件
    $("#eslCode").on('keypress', function(e) {
        var keycode = e.keyCode;
        var tm = $(this).val();
        if (keycode == '13') {
            $("#eslCode").val(tm);
            $("#boxnum").select();
            saveAuto();
        }
    });

    //返回按钮
    function closeWin() {
        api.closeWin({});
    }
</script>

</html>
